const qiniu = require("qiniu");
const ACCESS_KEY = "UxXRnLJnRsC3AaEII661ZgPOtmcFeaXigeKbc85O";
const SECRET_KEY = "_5m492oDojUEH-hPt02JDFNmbU2VCpMZU8UY_inv";
const mac = new qiniu.auth.digest.Mac(ACCESS_KEY, SECRET_KEY);

const { Auth } = require("@middlewares/auth");
const AUTH_ADMIN = 16;

const { Resolve } = require("@lib/helper");
const res = new Resolve();
const { upload } = require("@lib/upload");
const Router = require("koa-router");

const router = new Router({
  prefix: "/api/v1",
});

// 创建回复
router.post("/upload/token", new Auth(AUTH_ADMIN).m, async (ctx) => {
  // console.log('mac', mac)
  const options = {
    scope: "boblog-v2",
    expires: 7200,
  };
  const putPolicy = new qiniu.rs.PutPolicy(options);
  ctx.response.status = 200;
  const data = {
    token: putPolicy.uploadToken(mac),
  };
  ctx.body = res.json(data);
});

// ==================================================///
/**
 * 上传单个文件到本地 public/uploads
 */
router.post("/upload/file2local", upload.single("file"), async (ctx) => {
  //   console.log("ctx.request.file", ctx.request.file);
  let data = {
    path: ctx.file.path,
  };
  ctx.body = res.json(data);
});

/**
 * 上传多个文件到本地
 */

router.post(
  "/upload/multiple-files",
  upload.fields([
    {
      name: "imgs",
      maxCount: 3,
    },
    // {
    //   name: "boop",
    //   maxCount: 2,
    // },
  ]),
  async (ctx) => {
    let imgList = ctx.request.files.imgs.map(item=>item.path)
    // console.log("ctx.request.files", ctx.request.files);
    // console.log("ctx.files", ctx.files);
    // console.log("ctx.request.body", ctx.request.body);
    ctx.body = res.json(imgList)
  }
);
module.exports = router;
